layui.use(['element', 'table', 'layer', 'form', 'laydate'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;

    //修改 table 字段
    table.on('edit(edit-table)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
        data = obj.data;
        value = obj.value;
        field = obj.field;
        url = $('.edit-table').data('url');
        key = $('.edit-table').data('key');
        $.post(url, {id: data[key], field: field, value: value}, function () {
            data[field] = value;
        })
    });

    //监听工具条
    table.on('tool(edit-table)', function (obj) {
        var data = obj.data;
        var url = $(this).data('url');
        var key = $('.edit-table').data('key');
        if (obj.event === 'del') {
            layer.confirm('真的要删除么', function (index) {
                $.post(url, {id: data[key]}, function (res) {
                    layer.close(index);
                    var msg = res.msg ? res.msg : "网络异常";
                    if (res.code == 200) {
                        obj.del();
                    }
                    layer.msg(msg);
                })
            });
        }
        if (obj.event === 'refuse') {
            layer.confirm('真的拒绝吗?', function (index) {
                $.post(url, {id: data[key]}, function (res) {
                    layer.close(index);
                    var msg = res.msg ? res.msg : "网络异常";
                    if (res.code == 200) {
                        window.location.reload();
                    }
                    layer.msg(msg);
                })
            });
        }
        if (obj.event === 'agree') {
            layer.confirm('真的确定吗?', function (index) {
                $.post(url, {id: data[key]}, function (res) {
                    layer.close(index);
                    var msg = res.msg ? res.msg : "网络异常";
                    if (res.code == 200) {
                        window.location.reload();
                    }
                    layer.msg(msg);
                })
            });
        }
    });

    //弹出层
    $(document).on('click', '.model-a', function (e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $.get(url, {}, function (str) {
            if (str.code == 0) {
                layer.alert('权限不足');
                return;
            }
            layer.open({
                type: 1,
                content: str //注意，如果str是object，那么需要字符拼接。
                , skin: 'min-width-400'
            });
        });

    });
    $(document).on('click', '.model-aaa', function (e) {
        e.preventDefault();
        var msg = $(this).data('msg')?$(this).data('msg'):"";
        layer.alert(msg);
    });

    //相册
    $(document).on('click', '.layer-img', function (e) {
        var json = {
            "title": $(this).attr('name'), //相册标题
            "id": 1, //相册id
            "start": 0, //初始显示的图片序号，默认0
            "data": [   //相册包含的图片，数组格式
                {
                    "alt": $(this).attr('name'),
                    "pid": 1, //图片id
                    "src": $(this).attr('src'), //原图地址
                    "thumb": $(this).attr('src') //缩略图地址
                }
            ]
        };
        layer.photos({
            photos: json //格式见API文档手册页
            // ,anim: 0 //0-6的选择，指定弹出图片动画类型，默认随机
        });
    });

    //监听删除
    $(document).on('click', '.ajax-delete', function (e) {
        e.preventDefault();
        var loading = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var msg = $(this).data('msg');
        var url = $(this).attr('href');
        //询问框
        layer.confirm(msg, {
                btn: ['确定', '取消'] //按钮
                , end: function () {
                    layer.close(loading);
                }
            }, function () {
                $.post(url, '', function (res) {
                    layer.close(loading);
                    msg = res.msg ? res.msg : '网络错误';
                    layer.msg(msg);
                    jumpUrl = res.jumpUrl;
                    if (res.code == 200) {
                        if (jumpUrl) {
                            window.location.href = jumpUrl;
                        } else {
                            window.location.reload();
                        }
                    }
                })
            }
        );
    });


    //监听提交
    form.on('submit(ajax)', function (data) {
        var loading = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var url = data.form.action;
        var method = data.form.method;

        $.ajax({
            type: method,
            url: url,
            data: data.field,
            dataType: 'json',
            success: function (res) {
                console.log(res)
                layer.close(loading);
                msg = res.msg ? res.msg : '网络错误';
                layer.msg(msg);
                jumpUrl = res.jumpUrl;
                if (res.code == 200) {
                    if (jumpUrl) {
                        window.location.href = jumpUrl;
                    } else {
                        window.location.reload();
                    }
                }
            },
            error: function (res) {
                layer.close(loading);
                layer.msg("网络错误");
            }
        });
        return false;
    });

    $(document).on("submit", ".ajax-form", function (e) {
        var url = $(this).attr("action");
        var method = $(this).attr("method");
        layer.load(); //上传loading
        $.ajax({
            type: method,
            url: url,
            data: $(this).serialize(),
            dataType: 'json',
            success: function (res) {
                layer.closeAll('loading'); //关闭loading
                msg = res.msg ? res.msg : '网络错误';
                layer.msg(msg);
                jumpUrl = res.jumpUrl;
                if (res.code == 200) {
                    if (jumpUrl) {
                        window.location.href = jumpUrl;
                    } else {
                        window.location.reload();
                    }
                }
            },
            error: function (res) {
                layer.closeAll('loading'); //关闭loading
                layer.msg("网络错误");
            }
        });
        return false;
    });

    //switch监听
    form.on('switch(switchAjax)', function (data) {
        var url = $(data.elem).data('url');
        $.post(url, {'types': data.elem.checked}, function (res) {

        });
    });

    // 商品分类联动
    form.on('select(category_one)', function (data) {
        var url = $(data.elem).data('url');
        $.post(url, {pid: data.value}, function (res) {
            var x = ''
            if (res.code == 200) {
                x += '<option value="">二级分类</option>';
                for (r in res.data) {
                    x += '<option value="' + res.data[r].id + '">' + res.data[r].name + '</option>';
                }
            } else {
                x += '<option value="">暂无分类</option>';
            }
            $('.category_two').html(x);
            form.render();
        })
    });
    form.on('select(category_two)', function (data) {
        var url = $(data.elem).data('url');
        $.post(url, {pid: data.value}, function (res) {
            var x = ''
            if (res.code == 200) {
                x += '<option value="">三级分类</option>';
                for (r in res.data) {
                    x += ' <option value="' + res.data[r].category_id + '">' + res.data[r].category_name + '</option>';
                }
            } else {
                x += '<option value="">暂无分类</option>';
            }
            $('.category_three').html(x);
            form.render();
        })
    });

    laydate.render({
        elem: '.laydate-date' //指定元素
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
    laydate.render({
        elem: '.laydate-date1' //指定元素
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
    laydate.render({
        elem: '.laydate-date2' //指定元素
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
    laydate.render({
        elem: '.laydate-date3' //指定元素
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
    laydate.render({
        elem: '.laydate-date4' //指定元素
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
    laydate.render({
        elem: '.laydate-time' //指定元素
        , type: 'time'
        , range: false
        , showBottom: false
        , theme: 'grid'

    });
    laydate.render({
        elem: '.laydate-time-start' //指定元素
        , type: 'time'
        , range: false
        // , showBottom: false
        , theme: 'grid'

    });
    laydate.render({
        elem: '.laydate-time-end' //指定元素
        , type: 'time'
        , range: false
        // , showBottom: false
        , theme: 'grid'
    });
});
$(document).ready(
    function () {
        try {
            $('select.chosen-select').chosen({
                no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
                search_contains: true,   // 从任意位置开始检索
            });
            $('.chosen-container').width('548px');
        } catch (e) {

        }
    }
);
